@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

echo ====================================
echo    Unit Choice Quiz System - Startup
echo ====================================
echo.

REM Set project root directory
set "PROJECT_ROOT=%~dp0"
set "VENV_PATH=%PROJECT_ROOT%venv"
set "BACKEND_PATH=%PROJECT_ROOT%backend"
set "FRONTEND_PATH=%PROJECT_ROOT%frontend"

REM Check if Python is available
echo [INFO] Checking Python installation...
python --version >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Python not found, please install Python first
    echo [HELP] Download Python from: https://www.python.org/downloads/
    pause
    exit /b 1
)
python --version > temp_py_version.txt 2>&1
set /p PYTHON_VERSION=<temp_py_version.txt
del temp_py_version.txt >nul 2>&1
echo [INFO] Python found

REM Check if Node.js is available
echo [INFO] Checking Node.js installation...
node --version >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Node.js not found, please install Node.js first
    echo [HELP] Download Node.js from: https://nodejs.org/
    pause
    exit /b 1
)
node --version > temp_node_version.txt 2>&1
set /p NODE_VERSION=<temp_node_version.txt
del temp_node_version.txt >nul 2>&1
echo [INFO] Node.js found

REM Check and create virtual environment
echo [INFO] Checking virtual environment...
if not exist "%VENV_PATH%" (
    echo [INFO] Virtual environment not found, creating new one...
    python -m venv "%VENV_PATH%"
    if errorlevel 1 (
        echo [ERROR] Failed to create virtual environment
        echo [HELP] Make sure you have sufficient permissions and disk space
        pause
        exit /b 1
    )
    echo [INFO] Virtual environment created successfully
) else (
    echo [INFO] Virtual environment already exists
)

REM Activate virtual environment
echo [INFO] Activating virtual environment...
if not exist "%VENV_PATH%\Scripts\activate.bat" (
    echo [ERROR] Virtual environment activation script not found
    echo [HELP] Try deleting the venv folder and run this script again
    pause
    exit /b 1
)

call "%VENV_PATH%\Scripts\activate.bat"
if errorlevel 1 (
    echo [ERROR] Failed to activate virtual environment
    pause
    exit /b 1
)
echo [INFO] Virtual environment activated

REM Check if requirements.txt exists
if not exist "%BACKEND_PATH%\requirements.txt" (
    echo [ERROR] requirements.txt not found in backend directory
    echo [HELP] Make sure you're running this script from the project root
    pause
    exit /b 1
)

REM Check and install backend dependencies
echo [INFO] Checking backend dependencies...
cd /d "%BACKEND_PATH%"

REM Check and install backend dependencies
echo [INFO] Installing backend dependencies...
pip install -r requirements.txt
if errorlevel 1 (
    echo [ERROR] Failed to install backend dependencies
    echo [HELP] Check your internet connection and try again
    pause
    exit /b 1
)
echo [INFO] Backend dependencies installed successfully

REM Go back to project root
cd /d "%PROJECT_ROOT%"

REM Check if port 8000 is already in use
echo [INFO] Checking if port 8000 is available...
netstat -an | findstr ":8000" >nul 2>&1
if not errorlevel 1 (
    echo [WARNING] Port 8000 is already in use, attempting to free it...
    taskkill /F /IM python.exe >nul 2>&1
    taskkill /F /IM uvicorn.exe >nul 2>&1
    timeout /t 2 /nobreak >nul
)

REM Start backend server
echo [INFO] Starting backend API server (port 8000)...
start "Backend Server" cmd /k "cd /d "%BACKEND_PATH%" && call "%VENV_PATH%\Scripts\activate.bat" && echo Starting backend server... && python -m uvicorn main:app --host 0.0.0.0 --port 8000 --reload"

REM Wait for backend to start
echo [INFO] Waiting for backend server to start...
timeout /t 5 /nobreak >nul
echo [INFO] Backend server should be starting, check the Backend Server window

REM Check if frontend directory exists
if not exist "%FRONTEND_PATH%" (
    echo [ERROR] Frontend directory not found: %FRONTEND_PATH%
    pause
    exit /b 1
)

REM Check if package.json exists
if not exist "%FRONTEND_PATH%\package.json" (
    echo [ERROR] package.json not found in frontend directory
    echo [HELP] Make sure the frontend directory is properly set up
    pause
    exit /b 1
)

REM Switch to frontend directory and install dependencies
echo [INFO] Checking frontend dependencies...
cd /d "%FRONTEND_PATH%"

REM Install frontend dependencies
echo [INFO] Installing frontend dependencies...
npm install >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Failed to install frontend dependencies
    echo [HELP] Check your internet connection and try again
    pause
    exit /b 1
)
echo [INFO] Frontend dependencies installed successfully

REM Go back to project root
cd /d "%PROJECT_ROOT%"

REM Check if port 3000 is already in use
echo [INFO] Checking if port 3000 is available...
netstat -an | findstr ":3000" >nul 2>&1
if not errorlevel 1 (
    echo [WARNING] Port 3000 is already in use, attempting to free it...
    taskkill /F /IM node.exe >nul 2>&1
    timeout /t 2 /nobreak >nul
)

REM Start frontend development server
echo [INFO] Starting frontend development server (port 3000)...
start "Frontend Server" cmd /k "cd /d "%FRONTEND_PATH%" && echo Starting frontend development server... && echo Please wait, this may take a moment... && npm run dev"

REM Wait for frontend to start
echo [INFO] Waiting for frontend server to start...
timeout /t 10 /nobreak >nul

echo.
echo ====================================
echo           Startup Complete!
echo ====================================
echo.
echo 🚀 Services Status:
echo   Frontend: http://localhost:3000
echo   Backend:  http://localhost:8000
echo   Admin:    http://localhost:8000/admin
echo.
echo 📝 Two windows should have opened:
echo   1. Backend Server (Python/FastAPI with Virtual Environment)
echo   2. Frontend Server (Node.js/Vite)
echo.
echo 💡 Tips:
echo   - Virtual environment is automatically activated for backend
echo   - Wait for "Local: http://localhost:3000" in Frontend window
echo   - Both servers support hot reload for development
echo   - Close server windows to stop services
echo.

echo Press any key to open browser...
pause >nul

REM Open browser
echo [INFO] Opening browser...
start http://localhost:3000

echo.
echo ✅ Browser opened! System is ready for use.
echo.
echo Services are running in separate windows.
echo Press any key to exit this startup window...
echo (Note: Closing this window will NOT stop the servers)
pause >nul